package com.anjie.powerproject.controller;

import com.alibaba.fastjson.JSON;
import com.anjie.powerproject.common.Result;
import com.anjie.powerproject.entity.Point;
import com.anjie.powerproject.pojo.dto.ModelDTO;
import com.anjie.powerproject.pojo.dto.ModelPredictCommandDTO;
import com.anjie.powerproject.service.ModelService;
import com.anjie.powerproject.service.PointService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 模型(不分能量电量模型，全部查出)的Controller
 */
@Controller
@RequestMapping("model")
public class ModelController {
    @Autowired
    ModelService modelService;

    @Autowired
    PointService pointService;

    /**
     * 根据id获取模型的信息，并跳转到查看界面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/getInfo/{id}")
    private String getPointInfo(@PathVariable("id") Integer id, Model model){
        com.anjie.powerproject.entity.Model model1 = modelService.getModelById(id);
        ModelDTO modelDTO=new ModelDTO();
        BeanUtils.copyProperties(model1,modelDTO);
        if(!model1.getDeviceCode().equals("")||model1.getDeviceCode()!=null){
            Point point=pointService.getPointByDevCode(model1.getDeviceCode());
            if(point!=null)
                modelDTO.setPointName(point.getPointName());
            else  modelDTO.setPointName("无对应点位");
            model.addAttribute("modelDTO",modelDTO);
        }else modelDTO.setPointName("无对应点位");

        return "modelInfo";
    }

    /**
     * 根据点位id获取模型，并跳转到模型修改页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/update/{id}")
    private String updatePoint1(@PathVariable("id") Integer id,Model model){
        com.anjie.powerproject.entity.Model model1 = modelService.getModelById(id);
        ModelDTO modelDTO=new ModelDTO();
        BeanUtils.copyProperties(model1,modelDTO);
        Point point=pointService.getPointByDevCode(model1.getDeviceCode());
        if(point!=null)
            modelDTO.setPointName(point.getPointName());
        else  modelDTO.setPointName("无对应点位");
//        System.out.println(modelDTO.toString());
        model.addAttribute("modelDTO",modelDTO);
        return "modelEdit";
    }

    @RequestMapping("/getAllModel")
    @ResponseBody
    public Result getAllModel(){
//        List<Model>
        return new Result(modelService.getAllModel(),true,1,"查询成功");
    }

    @RequestMapping(value = "/deleteById",params = {"id"})
    @ResponseBody
    public Result deleteModelById(Integer id) {
        if(modelService.deleteModelById(id))
            return new Result(null,true,1,"删除成功");
        else
            return new Result(null,false,0,"删除失败,数据库中无此模型，请检查数据库");
    }

    @RequestMapping(value = "/update", params = {"id","modelName","deviceCode","pointId","modelVersion"})
    @ResponseBody
    public Result updateModel(Integer id,String modelName,String deviceCode,Integer pointId,String modelVersion){
        if(1==modelService.updateModel(id,modelName,deviceCode,pointId,modelVersion))
            return new Result(null,true,1,"修改成功");
        else
            return new Result(null,false,0,"修改失败");
    }


    /**
     * 预测完把model.status变回 1
     */
    @RequestMapping(value = "modelpredictFeedback")
    @ResponseBody
    public Result predictBack(@RequestBody String modelIdStr) {
//        System.out.println("----------predictBack-----------");
        //预测完成，把status变回1
        int modelId = Integer.parseInt(JSON.parseObject(modelIdStr).get("modelId").toString());
        com.anjie.powerproject.entity.Model model=modelService.getModelById(modelId);
        model.setStatus(1);
        modelService.save(model);
        return new Result(null,true,1,"修改成功");
    }

}
